/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is
 *
 * java-concurrency-presentation:
 * concurrency presentation tutorial.
 *
 * The Initial Developer of the Original Code is
 * Marco Antonio Villalobos, Jr.
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s): None.
 *
 * ***** END LICENSE BLOCK *****
 */

package presentation.concurrency.indexer;

/**
 * Represents the location of a word in a file.
 *
 * This class is thread safe due to its immutability.
 */
public class Location {

    /** The name of the file containing the word. */
    private final String file;
    /** The line in this file where that word occurred. */
    private final int line;

    public Location(String file, int line) {
        this.file = file;
        this.line = line;
    }

    /**
     * Retrieves the name of the file containing the word.
     *
     * @return The name of the file containing the word.
     */
    public String getFile() {
        return file;
    }

    /**
     * Retrieves the line in this file where that word occurred.
     *
     * @return The line in this file where that word occurred.
     */
    public int getLine() {
        return line;
    }

    /**
     * Indicates whether some other object is "equal to" this one.
     *
     * @param   obj   the reference object with which to compare.
     * @return  <code>true</code> if this object is the same as the obj
     *          argument; <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;

        Location location = (Location) obj;

        if (line != location.line) return false;
        if (file != null ? !file.equals(location.file) : location.file != null) return false;

        return true;
    }

    /**
     * Returns a hash code value for the object. This method is
     * supported for the benefit of hashtables such as those provided by
     * <code>java.util.Hashtable</code>. 
     *
     * @return  a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int result = file != null ? file.hashCode() : 0;
        result = 31 * result + line;
        return result;
    }
}
